//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;

using CommunityServer.Components;

namespace CommunityServer.Controls
{
    /// <summary>
    /// Summary description for TrackbackMarkUp.
    /// </summary>
    public class TrackbackMarkUp : Control
    {
        static string tag = Environment.NewLine + "<!--" + Environment.NewLine + 
            "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" + Environment.NewLine +
            "xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" + "\n" + 
            "xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\">" + Environment.NewLine + 
            "<rdf:Description" + Environment.NewLine + 
            "rdf:about=\"{0}\"" + Environment.NewLine + 
            "dc:identifier=\"{0}\"" + Environment.NewLine + 
            "dc:title=\"{1}\"" + Environment.NewLine + 
            "trackback:ping=\"{2}\" />" + Environment.NewLine + 
            "</rdf:RDF>" + Environment.NewLine + 
            "-->" + Environment.NewLine;

		static string registrationName = "Trackback({0})";

        /// <summary>
        /// Renders the RDF trackback markup
        /// </summary>
        public TrackbackMarkUp()
        {
			
        }

        
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);

			if(IsValid)
			{
				string baseUrl = Globals.HostPath(Context.Request.Url);
				Page.RegisterStartupScript(string.Format(registrationName,PermaLink), string.Format(tag ,baseUrl+PermaLink,Title,baseUrl+PingUrl)) ;
			}
		}


        protected override void Render(HtmlTextWriter writer)
        {
            //replaced in favor of the register method in OnPreRender
            //if(IsValid)
            //{
            //   string baseUrl = Globals.HostPath(Context.Request.Url);
            //   writer.WriteLine(tag,baseUrl+PermaLink,Title,baseUrl+PingUrl);
            //}
        }

        private bool IsValid
        {
            get
            {
                return !Globals.IsNullorEmpty(Title) && !Globals.IsNullorEmpty(PermaLink) && !Globals.IsNullorEmpty(PingUrl);
            }
        }



        private string _title;
        /// <summary>
        /// Property Title (string)
        /// </summary>
        public string Title
        {
            get {  return this._title; }
            set {  this._title = value; }
        }

        private string _permaLink;
        /// <summary>
        /// Property PermaLink (string)
        /// </summary>
        public string PermaLink
        {
            get {  return this._permaLink; }
            set {  this._permaLink = value; }
        }

        private string _pingUrl;
        /// <summary>
        /// Property PingUrl (string)
        /// </summary>
        public string PingUrl
        {
            get {  return this._pingUrl; }
            set {  this._pingUrl = value; }
        }
    }
}
